But How Do I…?¶
Items¶
Get an Item¶
itemRegistry.getItem("My_Item")
# or
ir.getItem("My_Item")
itemRegistry.getItem("My_Item")
// or
ir.getItem("My_Item")
itemRegistry.getItem("My_Item")
// or
ir.getItem("My_Item")
My_Item
Get the state of an Item¶
items["My_Item"]
# or after importing anything within the ``core`` package
items.My_Item
# or
ir.getItem("My_Item").state
items["My_Item"]
// or
ir.getItem("My_Item").state
items["My_Item"]
// or
ir.getItem("My_Item").state
My_Item.state
Get the triggering Item¶
ir.getItem(event.itemName)
ir.getItem(event.itemName)
ir.getItem(event.itemName)
triggeringItem
Get the triggering Item’s name¶
event.itemName
event.itemName
event.itemName
triggeringItem.name
Get the triggering Item’s state¶
event.itemState
event.itemState
event.itemState
triggeringItem.state
Get the triggeringItem’s previous state¶
event.oldItemState
event.oldItemState
event.oldItemState
previousState
Get the received command¶
event.itemCommand
event.itemCommand
event.itemCommand
receivedCommand
Send a command to an Item¶
events.sendCommand("Test_SwitchItem", "ON")
# or
events.sendCommand(ir.getItem("Test_SwitchItem"), ON)
events.sendCommand("Test_SwitchItem", "ON")
// or
events.sendCommand(ir.getItem("Test_SwitchItem"), ON)
events.sendCommand("Test_SwitchItem", "ON")
// or
events.sendCommand(ir.getItem("Test_SwitchItem"), ON)
Test_SwitchItem.sendCommand(ON)
// or
sendCommand("Test_SwitchItem", "ON")
Send an update to an Item¶
events.postUpdate("Test_SwitchItem", "ON")
events.postUpdate("Test_SwitchItem", "ON")
events.postUpdate("Test_SwitchItem", "ON")
Test_SwitchItem.postUpdate(ON)
// or
postUpdate("Test_SwitchItem", "ON")
Stop a rule if the triggering Item’s state is NULL or UNDEF¶
if isinstance(items[event.itemName], UnDefType):
return
# do stuff
if (items[event.itemName].class == UnDefType.class) {
return
} else {
// do stuff
}
TODO
if (triggeringItem.state == NULL || triggeringItem.state == UNDEF) {
return
} else {
// do stuff
}
Convert a value to a state for comparison¶
items["String_Item"] == StringType("test string")
items["Number_Item"] > DecimalType(5)
items["Temperature_Item"] > QuantityType(u"55 °F")
event.itemState <= DecimalType(event.oldItemState.intValue() + 60)
event.itemState <= DecimalType(event.oldItemState.doubleValue() + 60)
event.itemState <= DecimalType(event.oldItemState.floatValue() + 60)
TODO
TODO
TODO
Convert DecimalType to an integer or float for arithmetic¶
int(str(items["Number_Item1"])) + int(str(items["Number_Item2"])) > 5
items["Number_Item1"].intValue() + items["Number_Item2"].intValue() > 5
float(str(items["Number_Item"])) + 5.5555 > 55.555
items["Number_Item"].floatValue() + 5.5555 > 55.555
TODO
TODO
TODO
Groups¶
Get the members or all members of a Group¶
# just direct members, which could include groups
ir.getItem("gTest").members
# iteratively, all child Items and the child Items of all child groups
ir.getItem("gTest").allMembers
// just direct members, which could include groups
ir.getItem("gTest").members
// iteratively, all child Items and the child Items of all child groups
ir.getItem("gTest").allMembers
// just direct members, which could include groups
ir.getItem("gTest").members
// iteratively, all child Items and the child Items of all child groups
ir.getItem("gTest").allMembers
// just direct members, which could include groups
gTest.members
// iteratively, all child Items and the child Items of all child groups
gTest.allMembers
Intersection of two groups¶
list_of_items = [item.name for item in itemRegistry.getItem("gDS_FamilyRoom").members if "gMotion_Sensor" in item.groupNames]
# or
list_of_items = [item for item in itemRegistry.getItem("gDS_FamilyRoom").members if item in itemRegistry.getItem("gMotion_Sensor").members]
# or
list_of_items = [item.name for item in itemRegistry.getItem("gMotion_Sensor").members if item in itemRegistry.getItem("gDownstairs").allMembers]
TODO
TODO
TODO
Iterate over members of a Group¶
for item in ir.getItem("gTest").members:
# do stuff
TODO
TODO
gTest.members.forEach[item |
// do stuff
]
Filter members of a group¶
Returns a list of Items, not a GroupItem
listOfMembers = filter(lambda item: item.state == ON, ir.getItem("gTest").members)
# or using a list comprehension
listOfMembers = [item for item in ir.getItem("gTest").members if item.state == ON]
TODO
TODO
Returns a GrouptItem
val listOfMembers = gTest.members.filter(GenericItem item | item.state == ON)
Get the first Item in a filtered list of Group members¶
my_item = filter(lambda item: item.state == ON, ir.getItem("gTest").members)[0]
# or using a list comprehension
my_item = [item for item in ir.getItem("gTest").members if item.state == ON][0]
TODO
TODO
val myItem = gTest.members.findFirst(item.state == ON)
Get first 5 Items from a filtered list of Group members¶
Returns a list of Items
my_items = filter(lambda item: item.state == OFF, ir.getItem("gTest").members)[0:5]
# or using a list comprehension
my_items = [item for item in ir.getItem("gTest").members if item.state == ON][0:5]
TODO
TODO
TODO
Get a sorted list of Group members matching a condition¶
Returns a list of Items
sorted_battery_level = sorted(battery for battery in ir.getItem("gBattery").getMembers() if battery.state < DecimalType(5), key = lambda battery: battery.state)
TODO
TODO
TODO
Get a list of values mapped from the members of a Group¶
Returns a list of strings
battery_levels = map(lambda lowBattery: "{}: {}".format(lowBattery.label, str(lowBattery.state) + "%"), ir.getItem("gBattery").members)
TODO
TODO
TODO
Perform an arithmetic reduction of values from members of a Group¶
Returns a value
# to use the add() method, the states must be of type QuantityType (`Units of Measure <https://www.openhab.org/docs/concepts/units-of-measurement.html>`_)
weekly_rainfall = reduce(lambda sum, x: sum.add(x), map(lambda rain: rain.state, ir.getItem("gRainWeeklyForecast").members))
TODO
TODO
TODO
Example with several functions using Group members¶
Returns a string
lowBatteryMessage = "Warning! Low battery alert:\n\n{}".format(",\n".join(map(lambda lowBattery: "{}: {}".format(lowBattery.label,str(lowBattery.state) + "%"), sorted(battery for battery in ir.getItem("gBattery").getMembers() if battery.state < DecimalType(5), key = lambda battery: battery.state))))
TODO
TODO
TODO
Miscellaneous¶
Single line comment¶
# this is a single line comment
// this is a single line comment
// this is a single line comment
// this is a single line comment
Multiline comment¶
'''
this is
a multiline
comment
'''
/*
this is
a multiline
comment
*/
/*
this is
a multiline
comment
*/
/*
this is
a multiline
comment
*/
View the names of an object’s attributes¶
# replace `object` with the object you'd like to introspect
log.debug("dir(object)=[{}]".format(dir(object)))
TODO
// replace `object` with the object you'd like to introspect
object.properties.each{log.warn("object: " + it)}
Not possible
View all symbols in a context¶
log.debug("dir()=[{}]".format(dir()))
Not possible
TODO
Not possible
Get the UID of a rule by name¶
ruleUID = filter(lambda rule: rule.name == "This is the name of my rule", rules.getAll())[0].UID
# or using a list comprehension
ruleUID = [rule for rule in rules.getAll() if rule.name == "This is the name of my rule"][0].UID
TODO
TODO
Not possible
Enable or disable a rule by UID¶
from core import osgi
ruleEngine = osgi.get_service("org.openhab.core.automation.RuleManager") or osgi.get_service("org.eclipse.smarthome.automation.RuleManager")
ruleEngine.setEnabled(ruleUID, True)# enable rule
ruleEngine.setEnabled(ruleUID, False)# disable rule
TODO
TODO
Not possible
Run a rule by UID¶
from core import osgi
ruleEngine = osgi.get_service("org.openhab.core.automation.RuleManager") or osgi.get_service("org.eclipse.smarthome.automation.RuleManager")
ruleEngine.runNow(ruleFunction.UID)
consider_conditions = True# consider the rule's Conditions
ruleEngine.runNow(ruleFunction.UID, considerConditions, {'name': 'EXAMPLE'})
TODO
TODO
Not possible
Get Thing Status¶
from org.eclipse.smarthome.core.thing import ThingUID
thing_status = things.get(ThingUID('kodi:kodi:familyroom')).status
var ThingUID = Java.type("org.eclipse.smarthome.core.thing.ThingUID");
var thingStatus = things.get(new ThingUID('kodi:kodi:familyroom')).status;
TODO
var thingStatus = getThingStatusInfo('kodi:kodi:familyroom').status
Enable/disable a Thing¶
from core.log import logging, LOG_PREFIX
log = logging.getLogger("{}.TEST".format(LOG_PREFIX))
from core import osgi
try:
from org.openhab.core.thing import Thing UID
except:
from org.eclipse.smarthome.core.thing import ThingUID
thing_manager = osgi.get_service("org.openhab.core.thing.ThingManager") or osgi.get_service("org.eclipse.smarthome.core.thing.ThingManager")
kodi_thing = things.get(ThingUID("kodi:kodi:familyroom"))
thing_manager.setEnabled(ThingUID("kodi:kodi:familyroom"), False)# disable Thing
log.debug("Disabled: isEnabled [{}], statusInfo [{}]".format(kodi_thing.isEnabled(), kodi_thing.statusInfo))
thing_manager.setEnabled(ThingUID("kodi:kodi:familyroom"), True)# enable Thing
log.debug("Enabled: isEnabled [{}], statusInfo [{}]".format(kodi_thing.isEnabled(), kodi_thing.statusInfo))
TODO
TODO
TODO
Read/Add/Remove Item metadata¶
See the examples in the module… core.metadata
TODO
TODO
Metadata can be added and removed, but not read
Use org.joda.time.DateTime¶
from org.joda.time import DateTime
start = DateTime.now()
var DateTime = Java.type("org.joda.time.DateTime");
start = DateTime.now()
import org.joda.time.DateTime
start = DateTime.now()
now
Pause a thread¶
from time import sleep
sleep(5)# the unit is seconds, so use 0.5 for 500 milliseconds
TODO
TODO
Thread::sleep(5000)// the unit is milliseconds
Use a timer¶
See the timer_example.py in the Script Examples for examples of using both Python threading.Timer and the openHAB createTimer Action.
TODO
TODO
TODO